# coding=utf-8

import requests
import csv
import os, sys,time, json
import datetime

from pyquery import PyQuery as pq
from tomorrow import threads
import random
#当前文件的路径
pwd = os.getcwd()
print(pwd)
project_path=os.path.abspath(os.path.dirname(pwd)+os.path.sep+"./")
sys.path.append(project_path)
sys.path.append(pwd)
print(project_path)

# from util.log_handler import LogHandler

# log = LogHandler('read_csv')

# 获取文件名
def get_filename(path):
	filename = os.path.basename(path)
	filename = filename.split('.')[0]
	return filename

# 获取当前执行程序的文件名
def get_execute_filename():
	return get_filename(__file__)

# log = LogHandler(get_execute_filename())

host = '192.168.50.75:5001'
# 调用接口
def post_qx_data(url, data):
	try:
		r = requests.post(host + '/api/v1/qx/'+url, data=data)
		return(r.text)
	except Exception as e:
		pass
		# log.error("api request fail: %s,"%(e, ),exc_info=True)

# 读取文件
def read_file(path):
	f = open(path, 'r')
	info = json.loads(f.read())
	f.close()
	return info

# 遍历文件夹下的所有文件
def list_file(path, ext='.jpg'):
    L=[]
    for root, dirs, files in os.walk(path):
        for file in files:
            file_ext = file.endswith(ext)
            if file_ext:
                fpath = os.path.join(root, file)
                L.append(fpath)
    return L

# 时间补全
def time_completion(time_str):
	# 当时间只有一位数时，在前面补0
	if len(time_str) == 1:
		time_str = '0' + time_str
	return time_str

# 把北京时间转换成UTC时间
def get_utc_time(str_time):
    str_time = datetime.datetime.strptime(str_time, '%Y-%m-%d %H:%M:%S')
    utc_time = str_time + datetime.timedelta(hours=-8)
    str_utc_time = datetime.datetime.strftime(utc_time, "%Y-%m-%d %H:%M:%S")
    return str_utc_time

# 气象数据时间
def decode_date_time(year, month, day, hour, time="15时0分"):
    t_arr = time.split("时")
    weather_hour = t_arr[0]
    weather_hour = time_completion(weather_hour)

    weather_minute = t_arr[1].split("分")[0]
    weather_minute = time_completion(weather_minute)

    str_time = year + "-" + month + "-" + day +" " + str(weather_hour) +":" + str(weather_minute) +":00"
    if weather_hour > hour:
        str_time = datetime.datetime.strptime(str_time, '%Y-%m-%d %H:%M:%S')
        yesterday = str_time + datetime.timedelta(-1)
        str_time = datetime.datetime.strftime(yesterday, "%Y-%m-%d %H:%M:%S")
    str_utc_time = get_utc_time(str_time)
    return str_utc_time

# 气象数据
def deal_weather_info(data, state, airport_4_code, year, month, day, hour):
	for i in data:
		# print(airport_4_code)
		# print(state)
		hour_tag = decode_date_time(year, month, day, hour, i['HourTag'])
		# print('时间：', hour_tag)
		value = i['value']
		# print('数值：', value)
		content = i['content']
		if not content:
			content=''
		# print('content：', content)
		data = {'airport_4_code': airport_4_code, 'state':state ,'hour_tag':hour_tag ,'content':content ,'value':value}
		post_qx_data('weather', data)

# 机场起飞数据
def deal_take_off(info, airport_4_code):
	for i in info:
		# print(i)
		# print(airport_4_code)
		runway = i['RunWay']
		# print('起飞跑道：', runway)
		aircraft_type = i['AcraftType']
		# print('机型：', aircraft_type)
		hirl_rcls = i['HirlRcls']
		# print('起飞最低标准：', hirl_rcls)
		hirl = i['Hirl']
		# print('Hirl：', hirl)
		hirl_rcls_stop = i['HirlRclsStop']
		# print('HirlRclsStop:', hirl_rcls_stop)
		lights = i['Lights']
		# print('Lights:', lights)
		data = {'airport_4_code': airport_4_code, 'runway': runway, 'aircraft_type': aircraft_type, 'hirl_rcls': hirl_rcls, 'hirl': hirl,
		 		'hirl_rcls_stop': hirl_rcls_stop, 'lights': lights}

		post_qx_data('take_off', data)

# 机场着陆数据
def deal_landing(info, airport_4_code):
	for i in info:
		# print(airport_4_code)
		runway = i['RunWay']
		# print('着陆跑道：', runway)
		come_near_type = i['ComeNearType']
		# print('ComeNearType:', come_near_type)
		come_near_normal = i['ComeNearNormal']
		# print('ComeNearNormal:', come_near_normal)
		c_kind = i['CKind']
		# print('CKind:', c_kind)
		data = {'airport_4_code': airport_4_code, 'runway': runway, 'come_near_type': come_near_type, 'come_near_normal': come_near_normal,
				'c_kind': c_kind}
		post_qx_data('landing', data)

# 获取昨天的年月日
def get_yesterday(year, month, day):
    str_today = year + '-' + month + '-' + day
    today = datetime.datetime.strptime(str_today, '%Y-%m-%d')
    yesterday = today + datetime.timedelta(-1)
    str_yesterday = datetime.datetime.strftime(yesterday, "%Y-%m-%d")
    return str_yesterday

# 获取报文时间
def get_message_time(content, year, month, day, hour):
		message_time = content.split(' ')[2] # 1692 2112 2019083015 
		# 时间有可能是在第2、第3、第4个字符串
		if not message_time.endswith('Z'):
			message_time = content.split(' ')[1]
			if not message_time.endswith('Z'):
				message_time = content.split(' ')[3]
		message_day = message_time[0:2]
		message_hour = message_time[2:4]
		message_minute = message_time[4:6]
		# 判断日期是否是当天
		if message_day == day:
			return year + "-" + month + "-" + message_day + " " + message_hour + ":" + message_minute + ":00"
		# 日期是昨天
		else:
			yesterday = get_yesterday(year, month, day)
			return yesterday + " " + message_hour + ":" + message_minute + ":00"

# 报文信息
def deal_message(info, state, airport_4_code, year, month, day, hour):
	for i in info:
		# print(airport_4_code)
		content = i['content']
		# print('报文内容：', content)
		# 报文有时为空,如:2019090207/ZSNJ.json
		try:
			hour_tag = get_message_time(content, year, month, day, hour)
			# print('时间：', hour_tag)
			value = i['value']
			# print('value:', value)
			data = {'airport_4_code': airport_4_code, 'content': content, 'hour_tag': hour_tag, 'value': value, 'state': state}
			post_qx_data('message',data)
			# create_message(data)
		except Exception as e:
			pass
			# log.error("报文内容为空: %s,"%(e, ))

# 报文信息
# @threads(8)
def deal_json_data(airport_4_code, info, year, month, day, hour):
	# 气象数据
	for li in shuzhi_lists:
		name=li[0]
		state=li[1]
		deal_weather_info(info[name], state, airport_4_code, year, month, day, hour)

	# 起飞数据
	# departure_data = info['airportDepartureStandardList']['rows']
	# deal_take_off(departure_data, airport_4_code)

	# 着陆数据
	# arrival_data = info['airportArrivalStandardList']['rows']
	# deal_landing(arrival_data, airport_4_code)

	# 报文数据
	for li in baowen_lists:
		name=li[0]
		state=li[1]
		deal_message(info[name]['rows'], state, airport_4_code, year, month, day, hour)

# 文件夹的名字是下载json文件的时间，这里获取到下载时间的年月日
def get_time(path):
	time_str = path.split('/')[-2]
	year = time_str[0:4]
	month = time_str[4:6]
	day = time_str[6:8]
	hour = time_str[8:]
	return (year, month, day, hour)

# 处理json数据
@threads(8)
def deal_json(file_path):
	airport_4_code = get_filename(file_path) # 文件名是机场4字代码
	info = read_file(file_path)
	year, month, day, hour = get_time(file_path)
	dir_name = file_path.split('/')[-2]
	# log.info('%s, %s' % (dir_name, airport_4_code))
	print('%s, %s' % (dir_name, airport_4_code))

	# 向接口发送数据
	deal_json_data(airport_4_code, info, year, month, day, hour)

def get_list_dir(base_dir):
	today = datetime.datetime.today()
	yesterday = today + datetime.timedelta(-1)
	str_yesterday = datetime.datetime.strftime(yesterday, "%Y%m%d")
	dir_path = os.path.join(base_dir, str_yesterday)
	list_dir = []
	list_hour = ['02', '06', '10', '14', '18', '22']
	for hour in list_hour:
		list_dir.append(dir_path+hour)
	return list_dir


# 气象数据 1：气压  2：露点  3：温度  4：能见度  5：风速
shuzhi_lists = [['AirPressure', 1], ['DewPoint', 2], ['Temperature', 3], ['Visibility', 4], ['WindSpeed', 5]]
# 报文数据 1：机场实况报文  2：机场预报报文
baowen_lists = [['metarContentList', 1], ['tafContentList', 2]]

if __name__ == '__main__':
	base_dir = '/home/ubuntu/workspace/spider_data/xiaofeixiang/json_data'
	try:
		list_dir = get_list_dir(base_dir)
		list_dir.sort()
		for dir_path in list_dir:
			files = list_file(dir_path, '.json')
			files.sort()
			for file_path in files:
					deal_json(file_path)
	except Exception as e:
		pass
		# log.error(e)
